﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace m3u8Downloader
{
    public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
                    this.textBox1.Text = this.folderBrowserDialog1.SelectedPath.Trim();
            }
        }

      

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
               
            }
            else {
               
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string text = this.richTextBox1.Text;
            text=text.Replace("\n", "");
            String[] arr = Regex.Split(text, ".ts");

            List<string> all_list = new List<string>();

            for (int i = 0; i < arr.Length; i++)
            {
                
                if (arr[i].Trim().Length > 0)
                {
                    all_list.Add(arr[i] + ".ts");
                }
            }

             
            string dir = this.textBox1.Text;

            
            

            if (dir.Length == 0) {
                MessageBox.Show("下载目录不能为空", "提示", MessageBoxButtons.OK);
                return;
            }

            int per = 15;

            int d = (int)Math.Ceiling((double)all_list.Count / per);

            for (int i = 0; i < d; i++) {
                             int start = i * per;
                int end = (i + 1) * per;
                List<string> list = new List<string>();
                for (int j = start; j < end; j++)
                {
                    if (j < all_list.Count)
                    {
                        Console.WriteLine(all_list[j]);
                        list.Add(all_list[j]);
                    }
                }
                //Console.WriteLine("list长度:"+list.Count);
                bool last = false;
                if (i == (d - 1)) {
                    last = true;
                }

                //创建ThreadTest类的一个实例
                ThreadTest test = new ThreadTest(all_list, list, dir,last);
                //调用test实例的MyThread方法
                Thread thread = new Thread(new ThreadStart(test.MyThread));
                //启动线程
                thread.Start();



            }




        }
    }

    class ThreadTest
    {

        List<string> list = new List<string>();
        string dir = "";
        bool last = false;
        List<string> all_list = new List<string>();
        string output_file = "out.mp4";

        public ThreadTest(List<string> all_list1, List<string> list1, string dir1, bool last1) {
            
            all_list = all_list1;
            list = list1;
            dir = dir1;
           
            last = last1;

        }

        public void MyThread()
        {
            //Console.WriteLine("长度2:" + list.Count);
            //Console.WriteLine("这是一个实例方法");
            string output = "";
            for (int i = 0; i < list.Count; i++)
            {
                output = "";
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();

               
                startInfo.FileName = Environment.CurrentDirectory + "\\wget\\" + "wget.exe";

                string url = list[i];

                Console.WriteLine(i+" command: wget "+ " -P " + dir +" "+ url+" --no-check-certificate");

                startInfo.Arguments = " -P " + dir +" "+ url + " --no-check-certificate";
                startInfo.UseShellExecute = false;
                //startInfo.RedirectStandardInput = false;
                //startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                process.StartInfo = startInfo;
                //startInfo.Verb = "RunAs";
                process.Start();

                bool fin = false;
                if (i == (list.Count - 1))
                {
                    fin = true;
                }

                
                //process.WaitForExit();
                //process.Close();

                if (last && fin)
                    {

                    process.WaitForExit();
                    Console.WriteLine("完毕");
                      
                    if (output_file.Length > 0)
                    {
                        


                    }

                }
            }

            


        }


        public void generateMP4() {

            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();

            string filenames = "";
            for (int k = 0; k < all_list.Count; k++)
            {
                string[] f_arr = all_list[i].Split('/');
                string f_name = f_arr[f_arr.Length - 1];
                filenames += " -i " + dir + "\\" + f_name;

            }
            if (filenames.Length > 0)
            {
                //filenames = filenames.Substring(0, filenames.Length - 1);
            }

            //start ffmpeg process
            


            startInfo.FileName = "ffmpeg ";


            startInfo.Arguments = " " + filenames + " -vcodec copy -f mp4 " + output_file;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            process.StartInfo = startInfo;
            startInfo.Verb = "RunAs";


            process.Start();

        }







}
}
